<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Order extends Zend_Db_Table{
	
	protected $_name    = "orders";
    protected $_primary = "id";
    
    public function saveData($data, $items){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('orders', $data);
            
            $id = $db->lastInsertId("orders");
            
            foreach($items as $item){
                $item['order_id'] = $id;
                $item['product_id'] = $item['id'];
                
                unset($item['id']);
                
                $db->insert('order_details', $item);
            }
            
            $db->commit();
            
            return $id;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function saveOrderDetail($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('order_details', $data);
            
            $db->lastInsertId("order_details");
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getData($order_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('pk' => 'product_keys'), array('id', 'product', 'key'))
                    ->where('pk.product = ?', $product_id)
                    ->where('pk.is_used = 0')
                    ->limit($quantity);
        
        $result = $db->fetchAll($select);
        
        
        
        return $result;
    }
    
    public function getOrderDetail($order_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('o' => 'orders'), array('id', 'fullname', 'address', 'email', 'phone_number'))
                    ->where('o.id = ?', $order_id, "INTEGER");
        
        $result = $db->fetchRow($select);
        
        return $result;
    }
    
    public function updatePayment($order_id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $data['id'] = $order_id;
            $data['payment'] = true;
            
            $db->update('orders', $data, "id = $order_id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function updateStatus($order_id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $data['id'] = $order_id;
            $data['status'] = true;
            
            $db->update('orders', $data, "id = $order_id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkExists($id){
        $option = array('table' => 'orders', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
    
    public function setOrderIsSent($id){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            
            $db->beginTransaction();
            
            try{
                $data['id'] = $id;
                $data['is_sent'] = true;
                
                $db->update('orders', $data, "id = $id");
                $db->commit();
                
                return true;
            }catch(Exception $e){
                $db->rollBack();
                
                return false;
            }
    	}
    }
}